// ************************* 78 character ruler *****************************
// UsbDioControl.c -- Implementation of FAHA RFBB 
//
// $Author: Liu Rongwu $
// $Header: 
// $History: 09/25/2010 create, and modify by lh
//           02/05/2011 modify by Pedro Wang (En Qi) 
//
// (c) 2008 Nokia Siemens Networks.
// ************************* 78 character ruler *****************************

#include "RFBB.h"         
#include "UsbDioControl.h"
#include <utility.h>
#include "USB6501.h"


// Global variables
//------------------------------------------------------------------------
TaskHandle g_hTaskFlashModeActive = 0; // Port1,line0: FLASH_MODE     (DO)
TaskHandle g_hTaskBscanTestActive = 0; // Port1,line1: BSCAN_ON	      (DO)  
//TaskHandle g_hTaskFixtRdyIN = 0; // Port2,line0: Fixture Ready Signal (DI) 
//TaskHandle g_hTaskLMPConnectDut = 0; // Port2,line2: LMPConnect		  (DO)
//TaskHandle g_hTaskLMPDisConnectDut = 0; // Port2,line3: LMPDisConnect (DO)
//TaskHandle g_hTaskFiberOpticOn = 0; // Port2,line5: FiberOpticOn      (DO)
//TaskHandle g_hTaskFiberOpticOff = 0; // Port2,line5: FiberOpticOff    (DO)
//TaskHandle g_hTaskEACOn = 0; // Port2,line6: FiberOpticOn      (DO)
//TaskHandle g_hTaskEACOff = 0; // Port2,line5: FiberOpticOff    (DO)



// -----------------------------------------------------------------------
// Function:   InitialiseDio   
//             
//
int __decldllexp TX_TEST InitialiseDio(tTestData *testData, 
                                                   tTestError *testError)
{
   int iRet = 0;
   int error = 0;          // required for tsErrChk
   ErrMsg errMsg = {'\0'}; // required for tsErrChk
   
   unsigned int size = 0;
   unsigned char szDeviceName[255] = {'\0'}; 
   char szLogMsg[BUF_SIZE_SMALL] = {'\0'};
   
   // USB DIO initialisations
   sprintf( szLogMsg, "Init NIDAQmx USB-DIO..." );
   ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg));

   error = RegReadString (REGKEY_HKLM, "SOFTWARE\\NSN\\SWIFT\\Instruments\\DIO", 
         "DeviceName", szDeviceName, sizeof(szDeviceName), &size);
   if( error )
   {
      sprintf(errMsg, "DIO DeviceName error!");
      goto Error;
   }

   DAQmxResetDevice ("DIO1");
   Delay(2.0);

   error = InitUsbDio(szDeviceName);
   
Error:
   // FREE RESOURCES
   
    if ( error < 0 )
    {
        ErrChk(EELog_INEventLogLogEvent ( g_hEventLog, NULL, errMsg ));
        iRet = error;
    }
   
    return iRet;
}

// -----------------------------------------------------------------------
// Function:   CleanupDio   
//             
//
void __decldllexp TX_TEST CleanupDio(tTestData *testData, 
                                                   tTestError *testError)
{
   _DAQMX_WRITE_PORT_CMD g_szClearupPortCmd[6] = {{"ClearupDIO1_Port0","DIO1/port0",0xff},
                                                  {"ClearupDIO1_Port1","DIO1/port1",0xff},
                                                  {"ClearupDIO1_Port2","DIO1/port2",0xff},
                                                  {"ClearupDIO2_Port0","DIO2/port0",0xff},
                                                  {"ClearupDIO2_Port1","DIO2/port1",0xff},
                                                  {"ClearupDIO2_Port2","DIO2/port2",0xff}};
   int i = 0;
                                                    
   // Clean up DIO
   EELog_INEventLogLogEvent (g_hEventLog, NULL, "Cleanup NIDAQmx USB-DIO..." );
   
   //CleanupUsbDio();
   for(i = 0;i < 6;i++)
   	{
        USBWriteDigToPort(g_szClearupPortCmd[i].ucTashName,g_szClearupPortCmd[i].ucDevport,g_szClearupPortCmd[i].uiWriteData);
   	}
   
   return;
}


// -----------------------------------------------------------------------
// Function:   InitUsbDio
// Purpose:    To check if USB DIO has been initialised
// Return:     non-zero if initialised, 0 if not
//             Initialize USB-6501 I/O tasks for control 

//		Handle for USB DIO in FRIG RFBB Fixture
//		=========================================
// 		kszChan0[0] =  g_hTaskFlashModeActive // FlashModeActiv             (DO)
// 		kszChan0[1] =  g_hTaskBscanTestActive // BscanTestActive	        (DO)
// 		kszChan0[2] =  g_hTaskDP1_2           // NC
// 		kszChan0[3] =  g_hTaskDP1_3           // NC
// 		kszChan0[4] =  g_hTaskDP1_4           // NC
// 		kszChan0[5] =  g_hTaskDP1_5           // NC
// 		kszChan0[6] =  g_hTaskDP1_6           // NC
// 		kszChan0[7] =  g_hTaskDP1_7           // NC

// 		kszChan1[0] =  g_hTaskFixtRdyIN       // Fixture Ready Signal       (DI)
// 		kszChan1[1] =  g_hTaskDP2_1           // NC
// 		kszChan1[2] =  g_hTaskLMPConnectDut   // LMP CONNECT WITH DUT	    (DO)
// 		kszChan1[3] =  g_hTaskLMPDisConnectDut// LMP DISCONNECT WITH DUT	(DO)
// 		kszChan1[4] =  g_hTaskFiberOpticOn    // SFP CONNECT WITH DUT		(DO)
// 		kszChan1[5] =  g_hTaskFiberOpticOff   // SFP DISCONNECT WITH DUT	(DO)
// 		kszChan1[6] =  g_hTaskDP2_6           // NC		
// 		kszChan1[7] =  g_hTaskDP2_7           // NC


int InitUsbDio( const char* pszDeviceName )
{
   int error = 0;
//   ErrMsg errMsg = {'\0'};
   
   char errBuff[ 2048 ] = {'\0'};
   int iRet = 0;
   int iSamplesWritten = 0;
   int i = 0;
   
    uInt8 uDataToWrite[1] = {0x0};
//	uInt8 uDataToWrite1[1]= {0x001};
   
//   uInt16 uDataToWrite[1] = {0x0000}; //00000000 00000000  // For RFBB DIO, Clear All Routes 
	
    // Channel parameters
    char kszChan0[8][ 256 ] = {'\0'};
    char kszChan1[8][ 256 ] = {'\0'};
    char kszChan2[8][ 256 ] = {'\0'};

	for( i = 0; i < 8; i++ )
	{
		sprintf( kszChan0[i], "%s/port0/line%d", pszDeviceName, i ); // "DIO2/port0/linex"
		sprintf( kszChan1[i], "%s/port1/line%d", pszDeviceName, i ); // "DIO2/port1/linex"
    	sprintf( kszChan2[i], "%s/port2/line%d", pszDeviceName, i ); // "DIO2/port2/linex"
    }


    // Create DIO Tasks
	DAQmxErrChk( DAQmxCreateTask ( "", &g_hTaskFlashModeActive ) );
	DAQmxErrChk( DAQmxCreateTask ( "", &g_hTaskBscanTestActive ) );
	//DAQmxErrChk( DAQmxCreateTask ( "", &g_hTaskFixtRdyIN ) );
	//DAQmxErrChk( DAQmxCreateTask ( "", &g_hTaskLMPConnectDut ) );
	//DAQmxErrChk( DAQmxCreateTask ( "", &g_hTaskLMPDisConnectDut ) );
	//DAQmxErrChk( DAQmxCreateTask ( "", &g_hTaskFiberOpticOn ) );
	//DAQmxErrChk( DAQmxCreateTask ( "", &g_hTaskFiberOpticOff ) );
	//DAQmxErrChk( DAQmxCreateTask ( "", &g_hTaskEACOn ) );
	//DAQmxErrChk( DAQmxCreateTask ( "", &g_hTaskEACOff ) );
	
	
	
	
    // Create Digital Output (DO) Channel for Flash_Mode::Port1,line0 (FLASH_MODE) 
    DAQmxErrChk (DAQmxCreateDOChan(g_hTaskFlashModeActive, kszChan1[0], "FLASH_MODE", DAQmx_Val_ChanPerLine));
    DAQmxErrChk (DAQmxSetChanAttribute (g_hTaskFlashModeActive, "", DAQmx_DO_OutputDriveType,DAQmx_Val_ActiveDrive));
    DAQmxErrChk (DAQmxWriteDigitalLines( g_hTaskFlashModeActive, 1, 1, 10.0, DAQmx_Val_GroupByChannel, uDataToWrite, &iSamplesWritten, 0 ));
		
	// Create DO channel for Bscan_ON::Port1,line1 (BSCAN_ON)
    DAQmxErrChk (DAQmxCreateDOChan (g_hTaskBscanTestActive, kszChan1[1], "BSCAN_ON", DAQmx_Val_ChanPerLine));
    DAQmxErrChk (DAQmxSetChanAttribute (g_hTaskBscanTestActive, "", DAQmx_DO_OutputDriveType, DAQmx_Val_ActiveDrive));
    DAQmxErrChk (DAQmxWriteDigitalLines(g_hTaskBscanTestActive, 1, 1, 10.0, DAQmx_Val_GroupByChannel,uDataToWrite, &iSamplesWritten, 0 ));
		
	/*// Create DI channel for Fixture Ready Signal::Port2, line0
	DAQmxErrChk( DAQmxCreateDIChan( g_hTaskFixtRdyIN, kszChan2[0], "FIXTURE_READY", DAQmx_Val_ChanPerLine ) );
	DAQmxErrChk( DAQmxSetChanAttribute( g_hTaskFixtRdyIN, "", DAQmx_DI_InvertLines, FALSE ) ); // Attrib FALSE => HI: On; LO: Off*/

	
	// Create DO channel for EAC RJ45::Port2, line0
	//DAQmxErrChk( DAQmxCreateDOChan( g_hTaskEACOn, kszChan2[6], "EAC_EN", DAQmx_Val_ChanPerLine ) );
    //DAQmxErrChk (DAQmxSetChanAttribute (g_hTaskEACOn, "", DAQmx_DO_OutputDriveType, DAQmx_Val_ActiveDrive));
    //DAQmxErrChk (DAQmxWriteDigitalLines(g_hTaskEACOn, 1, 1, 10.0, DAQmx_Val_GroupByChannel,uDataToWrite, &iSamplesWritten, 0 ));
	
	//DAQmxErrChk( DAQmxCreateDOChan( g_hTaskEACOff, kszChan2[7], "EAC_OFF", DAQmx_Val_ChanPerLine ) );
    //DAQmxErrChk (DAQmxSetChanAttribute (g_hTaskEACOff, "", DAQmx_DO_OutputDriveType, DAQmx_Val_ActiveDrive));
    //DAQmxErrChk (DAQmxWriteDigitalLines(g_hTaskEACOff, 1, 1, 10.0, DAQmx_Val_GroupByChannel,uDataToWrite, &iSamplesWritten, 0 ));
	//Delay(1);   
	
	
	/*// Create DO channel for LMP Connect::Port2, line2
	DAQmxErrChk( DAQmxCreateDOChan( g_hTaskLMPConnectDut, kszChan2[2], "LMP_EN", DAQmx_Val_ChanPerLine ) );
       DAQmxErrChk (DAQmxSetChanAttribute (g_hTaskLMPConnectDut, "", DAQmx_DO_OutputDriveType, DAQmx_Val_ActiveDrive));
       DAQmxErrChk (DAQmxWriteDigitalLines( g_hTaskLMPConnectDut, 1, 1, 10.0, DAQmx_Val_GroupByChannel,uDataToWrite, &iSamplesWritten, 0 ));*/

	// Create DO channel for LMP DisConnect::Port2, line3
	//DAQmxErrChk( DAQmxCreateDOChan( g_hTaskLMPDisConnectDut, kszChan2[3], "LMP_OFF", DAQmx_Val_ChanPerLine ) );
    //DAQmxErrChk (DAQmxSetChanAttribute (g_hTaskLMPDisConnectDut, "", DAQmx_DO_OutputDriveType, DAQmx_Val_ActiveDrive));
    //DAQmxErrChk (DAQmxWriteDigitalLines( g_hTaskLMPDisConnectDut, 1, 1, 10.0, DAQmx_Val_GroupByChannel,uDataToWrite, &iSamplesWritten, 0 ));

	// Create DO channel for SFP Connect::Port2, line5
	//DAQmxErrChk( DAQmxCreateDOChan( g_hTaskFiberOpticOn, kszChan2[5], "SFP_EN", DAQmx_Val_ChanPerLine ) );
    //DAQmxErrChk (DAQmxSetChanAttribute (g_hTaskFiberOpticOn, "", DAQmx_DO_OutputDriveType, DAQmx_Val_ActiveDrive));
    //DAQmxErrChk (DAQmxWriteDigitalLines (g_hTaskFiberOpticOn, 1, 1, 10.0, DAQmx_Val_GroupByChannel, uDataToWrite, &iSamplesWritten, 0));

	// Create DO channel for SFP DisConnect::Port2, line5
	//DAQmxErrChk( DAQmxCreateDOChan( g_hTaskFiberOpticOff, kszChan2[5], "SFP_OFF", DAQmx_Val_ChanPerLine ) );
    //DAQmxErrChk (DAQmxSetChanAttribute (g_hTaskFiberOpticOff, "", DAQmx_DO_OutputDriveType, DAQmx_Val_ActiveDrive));
    //DAQmxErrChk (DAQmxWriteDigitalLines(g_hTaskFiberOpticOff, 1, 1, 10.0, DAQmx_Val_GroupByChannel,uDataToWrite, &iSamplesWritten, 0 ));



   Error:

    if ( DAQmxFailed( error ) )
        DAQmxGetExtendedErrorInfo( errBuff, 2048 );

    if ( error < 0 )
    {
        ErrChk(EELog_INEventLogLogEvent ( g_hEventLog, NULL, errBuff ));
        iRet = -1;
    }

 
   // ** TODO: Error handling
   if( error < 0 ) {
      CleanupUsbDio();
      // Log Error
   	return error;   
   }
  
    return iRet;
}


// -----------------------------------------------------------------------
// Function:   CleanupUsbDio
// Purpose:    
//
//
void CleanupUsbDio( )
{
    //int iSamplesWritten = 0;
	//uInt8 uDataToWrite[1] = {0x001};

	if(g_hTaskFlashModeActive)
	{
		DAQmxStopTask( g_hTaskFlashModeActive );
		DAQmxClearTask( g_hTaskFlashModeActive );
		g_hTaskFlashModeActive = 0;
	}
	if(g_hTaskBscanTestActive)
	{
	   DAQmxStopTask( g_hTaskBscanTestActive );
		DAQmxClearTask( g_hTaskBscanTestActive );
		g_hTaskBscanTestActive = 0;
	}
	/*if(g_hTaskFixtRdyIN)
	{
		DAQmxStopTask( g_hTaskFixtRdyIN );
		DAQmxClearTask( g_hTaskFixtRdyIN );
		g_hTaskFixtRdyIN = 0;
	}*/
	/*if(g_hTaskLMPConnectDut)
	{
        DAQmxWriteDigitalLines( g_hTaskLMPConnectDut, 1, 1, 10.0, DAQmx_Val_GroupByChannel,uDataToWrite, &iSamplesWritten, 0 );
		DAQmxStopTask( g_hTaskLMPConnectDut );
		DAQmxClearTask( g_hTaskLMPConnectDut );
		g_hTaskLMPConnectDut = 0;
	}*/
	/*if(g_hTaskLMPDisConnectDut)
	{
		DAQmxStopTask( g_hTaskLMPDisConnectDut );
		DAQmxClearTask( g_hTaskLMPDisConnectDut );
		g_hTaskLMPDisConnectDut = 0;
	}*/
	/*if(g_hTaskFiberOpticOn)
	{
		DAQmxWriteDigitalLines (g_hTaskFiberOpticOn, 1, 1, 10.0, DAQmx_Val_GroupByChannel, uDataToWrite, &iSamplesWritten, 0);
		DAQmxStopTask( g_hTaskFiberOpticOn );
		DAQmxClearTask( g_hTaskFiberOpticOn);
		g_hTaskFiberOpticOn = 0;
	}*/
	/*if(g_hTaskFiberOpticOff)
	{
		DAQmxStopTask( g_hTaskFiberOpticOff );
		DAQmxClearTask( g_hTaskFiberOpticOff );
		g_hTaskFiberOpticOff = 0;
	}*/
	/*if(g_hTaskEACOn)
	{
		DAQmxStopTask( g_hTaskEACOn );
		DAQmxClearTask( g_hTaskEACOn );
		g_hTaskEACOn = 0;
	}*/
	/*if(g_hTaskEACOff)
	{
		DAQmxStopTask( g_hTaskEACOff );
		DAQmxClearTask( g_hTaskEACOff );
		g_hTaskEACOff = 0;
	}*/

   return;
}

/*
// -----------------------------------------------------------------------
// Function:   PlugInEACRJ45
// Purpose:    
//
//
void PlugInEACRJ45( )
{
	int iSamplesWritten = 0;
	uInt8 uDataToWrite[1] = {0};	// EAC RJ45 Connect With Dut 
	char szLogMsg[BUF_SIZE_SMALL] = {'\0'};
	
	sprintf( szLogMsg, "Set Port2, line6: EAC RJ45 Plug in Dut" );
	EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
	   
	// EAC RJ45 Connect With Dut
	DAQmxWriteDigitalLines(g_hTaskEACOn, 1, 1, 10.0, DAQmx_Val_GroupByChannel,uDataToWrite, &iSamplesWritten, 0 );
	Delay(2.0);
	
    return; 
}
*/

/*
// -----------------------------------------------------------------------
// Function:   PlugOutEACRJ45
// Purpose:    
//
//
void PlugOutEACRJ45( )
{
	int iSamplesWritten = 0;
	uInt8 uDataToWrite[1] = {0x001};	// EAC RJ45 Disconnect With Dut 
	char szLogMsg[BUF_SIZE_SMALL] = {'\0'};
	
	sprintf( szLogMsg, "Set Port2, line6: EAC RJ45 Plug out Dut" );
	EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg);
	   
	// EAC RJ45 Disconnect With Dut
	DAQmxWriteDigitalLines(g_hTaskEACOn, 1, 1, 10.0, DAQmx_Val_GroupByChannel,uDataToWrite, &iSamplesWritten, 0 );
    Delay(1.0);
		
	return; 
}
*/

/*
// -----------------------------------------------------------------------
// Function:   ReleaseLMPandSFP   
//             
//
void __decldllexp TX_TEST ReleaseSFP(tTestData *testData, 
                                                   		tTestError *testError)
{
    int iSamplesWritten = 0;
	uInt8 uDataToWrite[1] = {0x001};
	
    // Release LMP And SFP
    EELog_INEventLogLogEvent (g_hEventLog, NULL, "Release SFP Interface..." );
   
	if(g_hTaskLMPConnectDut)
	{
        DAQmxWriteDigitalLines( g_hTaskLMPConnectDut, 1, 1, 10.0, DAQmx_Val_GroupByChannel,uDataToWrite, &iSamplesWritten, 0 );
	}
	
	Delay(0.5);
	
	if(g_hTaskFiberOpticOn)
	{
		DAQmxWriteDigitalLines (g_hTaskFiberOpticOn, 1, 1, 10.0, DAQmx_Val_GroupByChannel, uDataToWrite, &iSamplesWritten, 0);
	}
	
	Delay(1.0);	

   
   return;
}
*/


/*
// -----------------------------------------------------------------------
// Function:   ReadFixtureReady
// Purpose:    Read fixture ready line to verify it is closed 
// Return:		Read data value (0 / 1 / 999 - Bogus) or error (<0)
int __decldllexp ReadFixtureReady( void )
{

   int iRetData = 999;
   int error = 0;
   int iByteSamplesRead = 0;
   int iSamplesRead = 0;
   uInt8 uDataRead[1] = {0};
   char szLogMsg[BUF_SIZE_SMALL] = {'\0'};
   
   sprintf( szLogMsg, "Reading Port1, line0: Fixture Ready" );
   ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg));
   
      
   DAQmxErrChk( DAQmxReadDigitalLines (g_hTaskFixtRdyIN, DAQmx_Val_Auto, 10.0, DAQmx_Val_GroupByChannel,
							uDataRead, 1, &iSamplesRead, &iByteSamplesRead, 0));
   iRetData = uDataRead[0];
  
Error:
   // ** TODO: Error handling
   if( error < 0 ) {
      ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg));
      
      return error;
   }
   else

  	return iRetData; // return Read data 

}
*/

/*
// -----------------------------------------------------------------------
// Function:   LMPConnectDutOn
// Purpose:    Set DO to LMPConnectDutOn 
// Return:		Read data value (0 / 1 / 999 - Bogus) or error (<0)
int __decldllexp LMPConnectDutOn( void )
{

   int iRetData = 0;
   int error = 0;
   int iSamplesWritten = 0;
   uInt8 uDataToWrite[1] = {0};    // LMP Connect With Dut 
   char szLogMsg[BUF_SIZE_SMALL] = {'\0'};
   
   sprintf( szLogMsg, "Set Port2, line2: LMP Connect With Dut" );
   ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg));
      
   // Set LMP Connect With Dut
   DAQmxWriteDigitalLines( g_hTaskLMPConnectDut, 1, 1, 10.0, DAQmx_Val_GroupByChannel, 
                        	              uDataToWrite, &iSamplesWritten, 0 );  
   
   Delay(1); 
   

Error:
   // ** TODO: Error handling
   if( error < 0 ) {
      ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg));
      
      return error;
   }
   else 
  	  return iRetData;

}
*/

/*
// -----------------------------------------------------------------------
// Function:   LMPDisConnectWithDut
// Purpose:    Set DO to LMPDisConnectWithDut status 
// Return:		Read data value (0 / 1 / 999 - Bogus) or error (<0)
int __decldllexp LMPDisConnectWithDut( void )
{

   int iRetData = 0;
   int error = 0;
   int iSamplesWritten = 0;
   uInt8 uDataToWrite[0] = {0};    // LMP DisConnect With Dut 
   char szLogMsg[BUF_SIZE_SMALL] = {'\0'};
   
   sprintf( szLogMsg, "Set Port2, line3: LMP DisConnect With Dut" );
   ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg));
      
   // LMP DisConnect With Dut
   DAQmxWriteDigitalLines( g_hTaskLMPDisConnectDut, 1, 1, 10.0, DAQmx_Val_GroupByChannel, 
                        	              uDataToWrite, &iSamplesWritten, 0 );  
   Delay(1); 

Error:
   // ** TODO: Error handling
   if( error < 0 ) {
      ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg));
      
      return error;
   }
   else 
  	  return iRetData;

}
*/

/*
// -----------------------------------------------------------------------
// Function:   SFPConnectDutOn
// Purpose:    Set DO to LMPConnectDutOn 
// Return:		Read data value (0 / 1 / 999 - Bogus) or error (<0)
int __decldllexp FiberOpticOn( void )
{

   int iRetData = 0;
   int error = 0;
   int iSamplesWritten = 0;
   uInt8 uDataToWrite[1] = {0};    // FiberOptic Connect With Dut 
   char szLogMsg[BUF_SIZE_SMALL] = {'\0'};
   char kszCmonAgent[BUF_SIZE_SMALL] ={'\0'};
   
   g_hCmonAgent = RfbbInetTelnetOpen (g_szDUTIP, g_iPort, 0);

   //Set sfp model
   g_hShellCommand = RfbbInetTelnetOpen (g_szDUTIP, g_iShellCommandPort, 0); 
   sprintf(kszCmonAgent,"Memsh -w 0x9000c014 0x3fff7fff");
   ErrChk(ShellCommand_Action(kszCmonAgent, -1)); 
   InetTelnetClose(g_hShellCommand);

   //Prevent the SFP going to the network model
   sprintf(kszCmonAgent,g_szCmonSetSfpModel);
   ErrChk(CmonAgent_Action(kszCmonAgent, -1)); 

   sprintf( szLogMsg, "Set Port2, line5: FiberOptic Plug in Dut" );
   ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg));
      
   // FiberOptic Connect With Dut
   DAQmxWriteDigitalLines( g_hTaskFiberOpticOn, 1, 1, 10.0, DAQmx_Val_GroupByChannel, 
                        	              uDataToWrite, &iSamplesWritten, 0 );  

Error:
   InetTelnetClose(g_hCmonAgent);

   // ** TODO: Error handling
   if( error < 0 ) {
      ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg));
      
      return error;
   }
   else 
  	  return iRetData;

}
*/

/*
// -----------------------------------------------------------------------
// Function:   FiberOpticDetachWithDut
// Purpose:    Set DO to FiberOpticDetachWithDut 
// Return:		Read data value (0 / 1 / 999 - Bogus) or error (<0)
int __decldllexp FiberOpticDetachWithDut( void )
{

   int iRetData = 0;
   int error = 0;
   int iSamplesWritten = 0;
   uInt8 uDataToWrite[1] = {0};    // FiberOpticDetachOn With Dut 
   char szLogMsg[BUF_SIZE_SMALL] = {'\0'};
   
   sprintf( szLogMsg, "Set Port2, line2: FiberOpticDetachOn With Dut" );
   ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg));
      
   // Set FiberOpticDetachOn With Dut
   DAQmxWriteDigitalLines( g_hTaskFiberOpticOff, 1, 1, 10.0, DAQmx_Val_GroupByChannel, 
                        	              uDataToWrite, &iSamplesWritten, 0 );  

Error:
   // ** TODO: Error handling
   if( error < 0 ) {
      ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg));
      
      return error;
   }
   else 
  	  return iRetData;

}
*/

/*
// -----------------------------------------------------------------------
// Function:   LMPConnectDutOff
// Purpose:    Set DO to LMPConnectDutOff 
// Return:		Read data value (0 / 1 / 999 - Bogus) or error (<0)
int __decldllexp FiberOpticUnplug( void )
{

   int iRetData = 0;
   int error = 0;
   int iSamplesWritten = 0;
   uInt8 uDataToWrite[1] = {0x001};    // FiberOptic DisConnect With Dut 
   char szLogMsg[BUF_SIZE_SMALL] = {'\0'};
   
   sprintf( szLogMsg, "Set Port2, line4: FiberOptic Connect With Dut" );
   ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg));
      
   // FiberOptic Connect With Dut
   DAQmxWriteDigitalLines( g_hTaskFiberOpticOn, 1, 1, 10.0, DAQmx_Val_GroupByChannel, 
                        	              uDataToWrite, &iSamplesWritten, 0 );  

Error:
   // ** TODO: Error handling
   if( error < 0 ) {
      ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg));
      
      return error;
   }
   else 
  	  return iRetData;

}
*/

// -----------------------------------------------------------------------
// Function:   Set BscanTestActive
// Purpose:    Set DO to select Bscan_ON signal to off for booting
// Return:		Read data value (0 / 1 / 999 - Bogus) or error (<0)
int __decldllexp  BscanTestActive( void )
{

   int iRet = 0;
   int error = 0;
   int iSamplesWritten = 0;
   uInt8 uDataToWrite[1] = {1};
   char szLogMsg[BUF_SIZE_SMALL] = {'\0'};
   
   sprintf( szLogMsg, "Set Port1, line4,5: 1 to set BscanTestActive HI");
   ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg));
   
   
   DAQmxErrChk (DAQmxWriteDigitalLines( g_hTaskBscanTestActive, 1, 1, 10.0, DAQmx_Val_GroupByChannel, 
                        	         uDataToWrite, &iSamplesWritten, 0 ));
   
Error:
   // ** TODO: Error handling
   if( error < 0 ) {
      ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg));
      
      return error;
   }
   else 
  		return iRet;

}
// -----------------------------------------------------------------------
// Function:   Set FlashModeActive
// Purpose:    Set DO to select FlashModeActive signal to off for booting
// Return:		Read data value (0 / 1 / 999 - Bogus) or error (<0)
int __decldllexp  FlashModeActive( void )
{

   int iRet = 0;
   int error = 0;
   int iSamplesWritten = 0;
   uInt8 uDataToWrite[1] = {0};
   char szLogMsg[BUF_SIZE_SMALL] = {'\0'};
   
   sprintf( szLogMsg, "Set Port1, line0,5: 1 to set FlashModeActive HI!");
   ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg));
   																   
   DAQmxErrChk (DAQmxWriteDigitalLines( g_hTaskFlashModeActive, 1, 1, 10.0, DAQmx_Val_GroupByChannel, 
                        	         uDataToWrite, &iSamplesWritten, 0 ));
   
Error:
   // ** TODO: Error handling
   if( error < 0 ) {
      ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg));
      
      return error;
   }
   else 
  		return iRet;

}

// -----------------------------------------------------------------------
// Function:   Set Bscan OFF for booting
// Purpose:    Set DO to select Bscan_ON signal to off for booting
// Return:		Read data value (0 / 1 / 999 - Bogus) or error (<0)
int __decldllexp Set_Boot( void )
{

   int iRet = 0;
   int error = 0;
   int iSamplesWritten = 0;
   uInt8 uDataToWrite[1] = {1};
   char szLogMsg[BUF_SIZE_SMALL] = {'\0'};
   
   sprintf( szLogMsg, "Set Port1, line4,5: 1 to set Bscan_On HI,Flash_Mode HI for booting!");
   ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg));
   
   
   DAQmxErrChk (DAQmxWriteDigitalLines( g_hTaskBscanTestActive, 1, 1, 10.0, DAQmx_Val_GroupByChannel, 
                        	         uDataToWrite, &iSamplesWritten, 0 ));
   DAQmxErrChk (DAQmxWriteDigitalLines( g_hTaskFlashModeActive, 1, 1, 10.0, DAQmx_Val_GroupByChannel, 
                        	         uDataToWrite, &iSamplesWritten, 0 ));
   
Error:
   // ** TODO: Error handling
   if( error < 0 ) {
      ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg));
      
      return error;
   }
   else 
  		return iRet;

}

// -----------------------------------------------------------------------
// Function:   Set BscanCSSA
// Purpose:    Set DO to Bscan_ON and Flash_Mode signal to select CSSA chain
// Return:		Read data value (0 / 1 / 999 - Bogus) or error (<0)
int __decldllexp  Set_BscanCSSA( void )
{

   int iRet = 0;
   int error = 0;
   int iSamplesWritten = 0;
   uInt8 uDataToWrite1[1] = {1};
   uInt8 uDataToWrite2[1] = {0};
   char szLogMsg[BUF_SIZE_SMALL] = {'\0'};
   
   sprintf( szLogMsg, "Set Port1, line4,5: 1,0 to set Bscan_On HI,Flash_Mode LO for CSSA chain only!");
   ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg));
   
   
   DAQmxErrChk (DAQmxWriteDigitalLines( g_hTaskBscanTestActive, 1, 1, 10.0, DAQmx_Val_GroupByChannel, 
                        	         uDataToWrite1, &iSamplesWritten, 0 ));
   DAQmxErrChk (DAQmxWriteDigitalLines( g_hTaskFlashModeActive, 1, 1, 10.0, DAQmx_Val_GroupByChannel, 
                        	         uDataToWrite2, &iSamplesWritten, 0 ));
   
Error:
   // ** TODO: Error handling
   if( error < 0 ) {
      ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg));
      
      return error;
   }
   else 
  		return iRet;

}

// -----------------------------------------------------------------------
// Function:   Set BscanWholeChain
// Purpose:    Set DO to Bscan_ON and Flash_Mode signal to select whole chain
// Return:		Read data value (0 / 1 / 999 - Bogus) or error (<0)
int __decldllexp  Set_BscanWholeChain( void )
{

   int iRet = 0;
   int error = 0;
   int iSamplesWritten = 0;
   uInt8 uDataToWrite1[1] = {0};
   uInt8 uDataToWrite2[1] = {1};
   char szLogMsg[BUF_SIZE_SMALL] = {'\0'};
   
   sprintf( szLogMsg, "Set Port1, line4,5: 0,1 to set Bscan_On LO,Flash_Mode HI for whole bscan chain!");
   ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg));
   
   
   DAQmxErrChk (DAQmxWriteDigitalLines( g_hTaskBscanTestActive, 1, 1, 10.0, DAQmx_Val_GroupByChannel, 
                        	         uDataToWrite1, &iSamplesWritten, 0 ));
   DAQmxErrChk (DAQmxWriteDigitalLines( g_hTaskFlashModeActive, 1, 1, 10.0, DAQmx_Val_GroupByChannel, 
                        	         uDataToWrite2, &iSamplesWritten, 0 ));
   
Error:
   // ** TODO: Error handling
   if( error < 0 ) {
      ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg));
      
      return error;
   }
   else 
  		return iRet;

}

/*
int __decldllexp FiberOpticUnplug( void )
{

   int iRetData = 999;
   int error = 0;
   int iByteSamplesRead = 0;
   int iSamplesRead = 0;
   uInt8 uDataRead[1] = {0};
   char szLogMsg[BUF_SIZE_SMALL] = {'\0'};
   
   sprintf( szLogMsg, "Reading Port1, line0: Fixture Ready" );
   ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg));
   
      
   DAQmxErrChk( DAQmxReadDigitalLines (g_hTaskFixtRdyIN, DAQmx_Val_Auto, 10.0, DAQmx_Val_GroupByChannel,
							uDataRead, 1, &iSamplesRead, &iByteSamplesRead, 0));
   iRetData = uDataRead[0];
  
Error:
   // ** TODO: Error handling
   if( error < 0 ) {
      ErrChk(EELog_INEventLogLogEvent (g_hEventLog, NULL, szLogMsg));
      
      return error;
   }
   else

  	return iRetData; // return Read data 

}
*/

// ************************* 78 character ruler *****************************
//                            End of source file
// **************************************************************************
